﻿
/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Domain.IRepository.MainData;
using FriendStar.Repository.MainData;
using System;

namespace FriendStar.Application.MainData
{
    public class CPK_ZD_11_DatStoreHouseApp
    {
        private ICPK_ZD_11_DatStoreHouseRepository service = new CPK_ZD_11_DatStoreHouseRepository();
        private string userUnit = Configs.GetValue("UseUnit");
        string userCode = OperatorProvider.Provider.GetCurrent()==null?"" : OperatorProvider.Provider.GetCurrent().UserCode;
        string userName = OperatorProvider.Provider.GetCurrent() == null ? "" : OperatorProvider.Provider.GetCurrent().UserName;
        public List<CPK_ZD_11_DatStoreHouseEntity> GetList(string keyword = "")
        {
            var expression = ExtLinq.True<CPK_ZD_11_DatStoreHouseEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FStoreName.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.UseUnit).ToList();
        }

        public List<CPK_ZD_11_DatStoreHouseEntity> GetSelectList()
        {
            var expression = ExtLinq.True<CPK_ZD_11_DatStoreHouseEntity>();
            expression = expression.And(t => t.UseUnit == userUnit);
            return service.IQueryable(expression).OrderBy(t => t.FStoreNO).ToList();
        }       

        public List<CPK_ZD_11_DatStoreHouseEntity> GetStoreHouses(string storeType, string enableSign, string FStoreNO = "", string FStoreName = "", string FResPerson = "")
        {
           
            var expression = ExtLinq.True<CPK_ZD_11_DatStoreHouseEntity>();
            if (!string.IsNullOrEmpty(userUnit))
            {
                expression = expression.And(t => t.UseUnit == userUnit);
            }
            if (!string.IsNullOrEmpty(FStoreNO))
            {
                expression = expression.And(t => t.FStoreNO.Contains(FStoreNO));
            }
            if (!string.IsNullOrEmpty(storeType))
            {
                expression = expression.And(t => t.FStoreType.Contains(storeType));
            }
            if (!string.IsNullOrEmpty(FStoreName))
            {
                expression = expression.And(t => t.FStoreName.Contains(FStoreName));
            }
            if (!string.IsNullOrEmpty(FResPerson))
            {
                expression = expression.And(t => t.FResPerson.Contains(FResPerson));
            }
            if (!string.IsNullOrEmpty(enableSign))
            {
                expression = expression.And(t => t.FEnableSign == enableSign);
            }
            return service.IQueryable(expression).OrderBy(t => t.FStoreNO).ToList();
        }

        public void SubmitForm(CPK_ZD_11_DatStoreHouseEntity cpk_ZD_11_DatStoreHouseEntity)
        {
            var entity = service.FindEntity(p => p.FStoreNO == cpk_ZD_11_DatStoreHouseEntity.FStoreNO);
            if (entity != null)
            {
                cpk_ZD_11_DatStoreHouseEntity.FStoreID = entity.FStoreID;
                cpk_ZD_11_DatStoreHouseEntity.UseUnit = userUnit;
                cpk_ZD_11_DatStoreHouseEntity.FUpdateBy = userName;
                cpk_ZD_11_DatStoreHouseEntity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                cpk_ZD_11_DatStoreHouseEntity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(cpk_ZD_11_DatStoreHouseEntity);
            }
            else
            {
                cpk_ZD_11_DatStoreHouseEntity.FStoreID = Guid.NewGuid().ToString("D").ToUpper();
                cpk_ZD_11_DatStoreHouseEntity.UseUnit = userUnit;
                cpk_ZD_11_DatStoreHouseEntity.FCreateBy = userName;
                cpk_ZD_11_DatStoreHouseEntity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                cpk_ZD_11_DatStoreHouseEntity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(cpk_ZD_11_DatStoreHouseEntity);
            }
        }

        public void DeleteForm(string storeID)
        {
            service.Delete(p => p.FStoreID == storeID);
        }
    }
}
